package com.example.exceltosqlite.ui;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

import com.example.exceltosqlite.R;
import com.example.exceltosqlite.base.BaseActivity;
import com.example.exceltosqlite.bean.CommonBean;
import com.example.exceltosqlite.bean.MyConstant;
import com.hz.android.fileselector.DefaultFileIconCreator;
import com.hz.android.fileselector.FileContainsFieldsFilter;
import com.hz.android.fileselector.FileExtendFilter;
import com.hz.android.fileselector.FileSelectorView;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FileSelectActivity extends BaseActivity {

    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.txt_cur_path)
    TextView txtCurPath;
    @BindView(R.id.file_selector_view)
    FileSelectorView fileSelectorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_select);
        ButterKnife.bind(this);
        init();
    }


    private void init() {

        File curDir = new File(Environment.getExternalStorageDirectory(), "");
        txtCurPath.setText(curDir.getAbsolutePath());
        //切换目录
        fileSelectorView.setCurrentDirectory(curDir);

        //设置文件过滤
//        fileSelectorView.setFileFilter(new FileExtendFilter(Arrays.asList("txt"))); // 设置过滤规则
        fileSelectorView.setFileFilter(new FileContainsFieldsFilter(Arrays.asList("xls","xlsx")));


        //文件UP正序
//        fileSelectorView.setFileSortComparator(new FileSelectorView.FileAscSortComparator());

        //文件DOWN倒序
//        fileSelectorView.setFileSortComparator(new FileSelectorView.FileDesSortComparator());

       /* //自定义文件图标
        fileSelectorView.setFileIconFactory(new FileSelectorView.FileIconCreator() {
            public Drawable getIcon(File file) {
                if (file == null) {
                    return getResources().getDrawable(R.drawable.rotating);
                } else {
                    return getResources().getDrawable(R.drawable.layers3);
                }
            }
        });*/

        //自定义部分文件图标
        fileSelectorView.setFileIconCreator(new FileSelectorView.FileIconCreator() {

            @Override
            public Drawable getIcon(File file) {
                if (file == null) {
                    return getResources().getDrawable(R.mipmap.b_img_001);
                } else {
                    return new DefaultFileIconCreator(getApplicationContext()).getIcon(file); //走默认逻辑
                }
            }
        });

        //设置选择文件的监听
        fileSelectorView.setOnFileSelectedListener(new FileSelectorView.OnFileSelectedListener() {
            @Override
            public void onSelected(File selectedFile) {
                EventBus.getDefault().post(new CommonBean(MyConstant.EVENT_MSG_1, selectedFile.getAbsolutePath()));
                finish();
            }

            @Override
            public void onFilePathChanged(File file) {
                txtCurPath.setText(file.getAbsolutePath());
            }
        });
    }
}
